package com.algorithm;

import java.util.Arrays;

/**
 * @author: sun.hongliang
 * @create: 2019/12/10 18:00
 */
public class SortTest {
    public static void main(String[] args) {
        int[] iii = new int[]{9, 2, 0, 4, 5, 6};
        SortTest sortTest = new SortTest();
        sortTest.quickSort(iii);
        System.out.println(Arrays.toString(iii));
    }

    public void maopao(int[] nums) {
        for (int i = nums.length - 1; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                if (nums[j] > nums[j + 1]) {
                    int temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                }
            }
        }
    }

    public void quickSort(int[] nums) {
        if (nums.length == 0) {
            return;
        }
        sort(nums, 0, nums.length - 1);
    }

    private void sort(int[] nums, int start, int end) {
        if (start >= end) {
            return;
        }
        int i = start;
        int j = end;
        int base = nums[start];
        while (i < j) {
            while (i < j && nums[j] >= base) {
                j--;
            }
            while (i < j && nums[i] <= base) {
                i++;
            }
            if (i < j) {
                int t = nums[i];
                nums[i] = nums[j];
                nums[j] = t;
            }
        }
        nums[start] = nums[i];
        nums[i] = base;
        sort(nums, start, i - 1);
        sort(nums, i + 1, end);
    }
}
